<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Lang;

/**
 * 客户|业务 管理
 * Class Customer
 * @package app\api\controller
 * 
 * @method mixed excessCustomer()  		超标客户
 * @method mixed lostCustomer()			流失客户
 * @method mixed activeNotStands()		激活未达标
 * @method mixed process()				处理【激活未达标 | 流失客户 | 超标客户】
 * @method mixed getTagList()			标签切换内容
 * @method mixed newlyTakes()			新人有礼活动达标
 * @method mixed acActivityStands()		确认【处理】活动达标
 * @method mixed takesAllot()			礼品发放分配 - 业务操作
 * @method mixed bindGift()				绑定礼品
 * @method mixed feedback()				反馈列表
 * @method mixed ac_feedback()			确认反馈
 * @method mixed return_visit()			售后回访
 * @method mixed phoneSave()			号码保护
 */
class Customer extends Api
{
	protected $noNeedLogin = ['excessCustomer'];
	protected $noNeedRight = '*';

	/**
	 * 超标客户
	 */
	public function excessCustomer()
	{
		$typs = $this->request->post("typs", 2);
		$page = $this->request->post("page", 1);
		$limit = $this->request->post("limit", 20);
		$offset = ($page - 1) * $limit;
		$ret = \app\api\controller\datas\Querys::instance()->excessCustomer($typs, $offset, $limit);
		$this->success("超标客户", $ret);
	}

	/**
	 * 流失客户
	 */
	public function lostCustomer()
	{
		$typs = $this->request->post("typs", 2);
		$page = $this->request->post("page", 1);
		$limit = $this->request->post("limit", 20);
		$offset = ($page - 1) * $limit;
		$ret = \app\api\controller\datas\Querys::instance()->lostCustomer($typs, $offset, $limit);
		$this->success("流失客户", $ret);
	}

	/**
	 * 激活未达标
	 */
	public function activeNotStands()
	{
		$typs = $this->request->post("typs", 2);
		$page = $this->request->post("page", 1);
		$limit = $this->request->post("limit", 20);
		$offset = ($page - 1) * $limit;
		$ret = \app\api\controller\datas\Querys::instance()->activeNotStands($typs, $offset, $limit);
		$this->success("激活未达标", $ret);
	}

	/**
	 * 处理【激活未达标 | 流失客户 | 超标客户】
	 */
	public function process()
	{
		$ids = $this->request->post("ids", 0);
		$typs = $this->request->post("typs", 0);
		$remark = $this->request->post("remark", '');
		if (!$ids && !$typs) {
			$this->error(__('Invalid parameters'));
		}
		if (!in_array($typs, [1, 2, 3])) {
			$this->error(__('处理类型有误！！！'));
		}
		$uid = $this->auth->id;
		$nick = ($typs == 1 ? "激活未达标" : ($typs == 2 ? "流失客户" : ($typs == 3 ? "超标客户" : "其它")));
		$ret = \app\api\controller\business\Adds::instance()->process($uid, $ids, $typs, $remark);
		$this->success("处理" . $nick, $ret);
	}

	/**
	 * 标签切换内容
	 */
	public function getTagList()
	{
		$data = [];
		$data = \app\api\controller\business\Lists::instance()->tags($this->auth->is_typs);
		$this->success("切换标签", $data);
	}

	/**
	 * 新人有礼活动达标
	 */
	public function newlyTakes()
	{
		$typs = $this->request->post("typs", 2);
		$page = $this->request->post("page", 1);
		$limit = $this->request->post("limit", 20);
		$offset = ($page - 1) * $limit;
		$uid = $this->auth->id;
		$ret = \app\api\controller\business\Lists::instance()->newlyGift($uid, $typs, $offset, $limit);
		$this->success("新人有礼活动达标", $ret);
	}

	/**
	 * 确认【处理】活动达标
	 */
	public function acActivityStands()
	{
		$ids = $this->request->post("ids", 0);
		$remarks = $this->request->post("remark", '');
		$uid = $this->auth->id;
		$ret = \app\api\controller\business\Adds::instance()->actNewlyGiftActivity($ids, $uid, $remarks);
		$this->success("活动达标确认", $ret);
	}

	/**
	 * 礼品发放分配 - 业务操作
	 */
	public function takesAllot()
	{
		$ids = $this->request->post("ids", 0);
		$sid = $this->request->post("sid", 0);
		$ret = \app\api\controller\business\Adds::instance()->issueAllot($ids, $sid);
		$this->success("礼品发放分配", $ret);
	}

	/**
	 * 绑定礼品
	 */
	public function bindGift()
	{
		$ids = $this->request->post("ids", 0);
		$code = $this->request->post("codes", "");
		if (!$code) {
			$this->error(__('Invalid parameters'));
		}
		$uid = $this->auth->id;
		$ret = \app\api\controller\business\Adds::instance()->bindGift($uid, $ids, $code);
		if ($ret === false) {
			$this->error(__('礼品绑定失败'));
		}
		$this->success("确认绑定", $ret);
	}

	/**
	 * 反馈列表
	 */
	public function feedback()
	{
		$typs = $this->request->post("typs", 2);
		$page = $this->request->post("page", 1);
		$limit = $this->request->post("limit", 20);
		$offset = ($page - 1) * $limit;
		$uid = $this->auth->id;
		$ret = \app\api\controller\business\Lists::instance()->feedback($uid, $typs, $offset, $limit);
		$this->success("反馈列表", $ret["data"]);
	}

	/**
	 * 确认反馈
	 */
	public function ac_feedback()
	{
		$ids = $this->request->post("ids");
		$remarks = $this->request->post("remarks", '');
		if (!$ids) {
			$this->error(__('Invalid parameters'));
		}
		$uid = $this->auth->id;
		$ret = \app\api\controller\business\Adds::instance()->ac_feedback($ids, $uid, $remarks);
		$this->success("确认反馈", $ret);
	}

	/**
	 * 回访
	 */
	public function return_visit()
	{
		$uid = $this->auth->id;
		$cid = $this->request->post("cid"); // 客户ID
		$typs = $this->request->post("typs"); // 问题类型
		$problem = $this->request->post("problem", ''); // 问题描述
		$process = $this->request->post("process", ''); // 处理描述
		if (!$typs && !$problem && !$process) {
			$this->error(__('Invalid parameters'));
		}
		$parms = ["uid"	=> $cid, "class_id" => $typs, "remark" => $problem, "process_des" => $process];
		$ret = \app\api\controller\business\Adds::instance()->visit($parms, $uid);
		if ($ret) {
			$this->success("回访问题提交成功", 1);
		}
		$this->error("回访问题提交失败");
	}

	/**
	 * 号码保护
	 */
	public function phoneSave()
	{
		$phone = $this->request->post("phone");
		$poolKey = "FC100000177746880";
		$res = Aliyuns::phoneSave($phone, $poolKey);
		if ($res["code"] == 1) {
			$res["phone"] = "17094741262";
		}
		return json($res);
	}
}
